package com.jinghang.wayofrich.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.jinghang.wayofrich.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 信用卡页面
 */
public class CardFragment extends Fragment {
    //银行列表
    private GridView bankView;
    private List<Map<String, Object>> bankList;
    private SimpleAdapter bankAdapter;


    //卡列表
    private ArrayList<HashMap<String, Object>> cardList;
    private SimpleAdapter cardListAdapter;
    private ListView cardView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_card,container,false);
        //初始化银行
        initBanks(view);
        //初始化银行卡
        initCards(view);
        return view;
    }

    /**
     * 初始化银行
     * @param view
     */
    private void initBanks(View view){
        bankView = view.findViewById(R.id.bankList);
        //初始化数据
        getBanks();
        String[] from={"bankImg","bankName","bankDesc"};

        int[] to={R.id.bankImg,R.id.bankName,R.id.bankDesc};

        bankAdapter=new SimpleAdapter(getContext(), bankList, R.layout.fragment_card_hotbank, from, to);

        bankView.setAdapter(bankAdapter);

        bankView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                AlertDialog.Builder builder= new AlertDialog.Builder(getContext());
                builder.setTitle("提示").setMessage(bankList.get(arg2).get("bankName").toString()).create().show();
            }
        });
    }

    /**
     * 获取银行数据
     */
    private void getBanks() {
        //图标
        int icno[] = { R.drawable.icon_zsbank, R.drawable.icon_zsbank, R.drawable.icon_zsbank,
                R.drawable.icon_zsbank, R.drawable.icon_zsbank, R.drawable.icon_zsbank, R.drawable.icon_zsbank,
                R.drawable.icon_zsbank, R.drawable.icon_zsbank};
        //图标下的文字
        String name[]={"招商银行","招商银行","招商银行","招商银行","招商银行","招商银行","招商银行","招商银行","招商银行"};

        String desc[] = {"招商银行","招商银行","招商银行","招商银行","招商银行","招商银行","招商银行","招商银行","招商银行"};
        bankList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i <icno.length; i++) {
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("bankImg", icno[i]);
            map.put("bankName",name[i]);
            map.put("bankDesc",desc[i]);
            bankList.add(map);
        }
    }


    /**
     * 初始化热卡
     * @param view
     */
    private void initCards(View view){
        //获取文章
        getCards(view);
        cardListAdapter = new SimpleAdapter(getContext(), cardList,// 数据源
                R.layout.fragment_card_hotcards,// ListItem的XML实现
                // 动态数组与ImageItem对应的子项
                new String[]{"cardTitle","cardDesc","cardApplyNum"},
                // ImageItem的XML文件里面的一个ImageView,TextView ID
                new int[]{R.id.cardTitle,R.id.cardDesc,R.id.cardApplyNum});
        // 添加并且显示
        cardView.setAdapter(cardListAdapter);
        // 添加菜单点击
        cardView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                for (int i = 0; i < cardList.size(); i++) {
                    if (arg2 == i) {
                        Toast.makeText(getContext(),
                                cardList.get(arg2).get("cardTitle") + "    " + arg2, Toast.LENGTH_SHORT)
                                .show();
                    }
                }
            }
        });
    }

    /**
     * 获取热卡数据
     */
    private void getCards(View view){
        cardView = view.findViewById(R.id.hotCardList);
        cardList = new ArrayList();
        for (int i=1;i<10;i++){
            HashMap<String, Object> map = new HashMap();
            map.put("cardTitle","广发车主卡");
            map.put("cardDesc", "加油88折，最高返现720元");
            map.put("cardApplyNum", "1946人本月申请");// 图像资源的ID
            cardList.add(map);
        }
    }
}
